import { defHttp } from '/@/utils/http/axios';
import { ErrorMessageMode } from '/#/axios';

enum Api {
  // 获取角色列表
  fetchRoleList = '/sys/role/page/list',
  // 创建角色
  createRole = '/sys/role/create',
  // 修改角色
  updateRole = '/sys/role/update',
  // 删除角色
  deleteRole = '/sys/role/delete',
  // 获取角色菜单权限配置
  roleMenu = '/sys/resource/menu',
  // 获取全部角色
  getAllRoleList = '/sys/role/list',
  // 获取用户角色
  getRoleListByUserId = '/sys/role/list',
  // 设置菜单权限
  updateRoleMenuAuthority = '/sys/resource/menu/update',
}

export function fetchRoleList(data, mode: ErrorMessageMode = 'modal') {
  const url = Api.fetchRoleList;
  return defHttp.post(
    { url: url, data },
    {
      errorMessageMode: mode,
    }
  );
}

export function createRole(data, mode: ErrorMessageMode = 'modal') {
  const url = Api.createRole;
  return defHttp.post(
    { url: url, data },
    {
      errorMessageMode: mode,
    }
  );
}

export function updateRole(data, mode: ErrorMessageMode = 'modal') {
  const url = Api.updateRole;
  return defHttp.post(
    { url: url, data },
    {
      errorMessageMode: mode,
    }
  );
}

export function deleteRole(id: string, mode: ErrorMessageMode = 'modal') {
  const url = `${Api.deleteRole}/${id}`;
  return defHttp.delete(
    { url: url },
    {
      errorMessageMode: mode,
    }
  );
}

export function getRoleMenuTree(roleId: number, mode: ErrorMessageMode = 'modal') {
  const url = `${Api.roleMenu}/${roleId}`;
  return defHttp.get(
    { url: url },
    {
      errorMessageMode: mode,
    }
  );
}

export function getAllRoleList(mode: ErrorMessageMode = 'modal') {
  const url = Api.getAllRoleList;
  return defHttp.get(
    { url: url },
    {
      errorMessageMode: mode,
    }
  );
}

export function getRoleListByUserId(userId: string, mode: ErrorMessageMode = 'modal') {
  const url = `${Api.getAllRoleList}/${userId}`;
  return defHttp.get(
    { url: url },
    {
      errorMessageMode: mode,
    }
  );
}

export function updateRoleMenuAuthority(data, mode: ErrorMessageMode = 'modal') {
  const url = Api.updateRoleMenuAuthority;
  return defHttp.put(
    { url: url, data },
    {
      errorMessageMode: mode,
    }
  );
}
